package com.juzhencms.apps.busi.web;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.juzhencms.apps.base.common.database.DBTable;
import com.juzhencms.apps.base.common.util.MD5Util;
import com.juzhencms.apps.base.common.util.StringUtil;
import com.juzhencms.apps.base.common.util.WebUtil;

@Controller
@RequestMapping("/sensitiveLog")
public class SensitiveLogController extends BaseController {

	// 查询所有rizhi 
	@RequestMapping(value = "/gridlistData", produces = "text/html; charset=UTF-8")
	@ResponseBody
	public String gridlistData(HttpServletRequest request) {
		Map data = new HashMap();
		List whereParamList = new ArrayList();
		String where = "where del_flag=0";

		if (request.getParameter("publish") != null && !request.getParameter("publish").toString().trim().equals("")) {
			where += " and publish = ? ";
			whereParamList.add(request.getParameter("publish"));
		}

		if (request.getParameter("status") != null
				&& !request.getParameter("status").toString().trim().equals("")) {
			where += " and status = ? ";
			whereParamList.add(request.getParameter("status"));
		}

		if (request.getParameter("sensitive_keywords") != null && !request.getParameter("sensitive_keywords").toString().trim().equals("")) {
			String name = request.getParameter("sensitive_keywords");
			where += " and sensitive_keywords like ? ";
			whereParamList.add("%" + request.getParameter("sensitive_keywords") + "%");
		}

		// 分页处理
		int pageIndex = 1;
		if (request.getParameter("pageIndex") != null
				&& !request.getParameter("pageIndex").toString().trim().equals("")) {
			pageIndex = new Integer(request.getParameter("pageIndex")).intValue();
		}
		int pageSize = 10;
		if (request.getParameter("pageSize") != null
				&& !request.getParameter("pageSize").toString().trim().equals("")) {
			pageSize = new Integer(request.getParameter("pageSize")).intValue();
		}
		int start = 0;
		if (pageIndex > 1) {
			start = (pageIndex - 1) * pageSize;
		}
		whereParamList.add(start);
		whereParamList.add(pageSize);

		// 排序参数
		String sortField = "id";
		String sortOrder = "desc";
		if (request.getParameter("sortField") != null && !request.getParameter("sortField").equals(""))
			sortField = request.getParameter("sortField");
		if (request.getParameter("sortOrder") != null && !request.getParameter("sortOrder").equals(""))
			sortOrder = request.getParameter("sortOrder");

		// 查询列表数据
		List dataList = this.getCommonService().getJdbcDao().querySql("select * from " + DBTable.sensitiveLog + " "
				+ where + " order by " + sortField + " " + sortOrder + " limit ?,?", whereParamList.toArray());
		if (dataList != null && dataList.size() > 0) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			for (int i = 0; i < dataList.size(); i++) {
				Map row = (Map) dataList.get(i);
				Date date = (Date) row.get("create_time");
				row.put("create_time", sdf.format(date));
				
				
			}
		}
		// 查询总条数
		Map countRow = this.getCommonService().getJdbcDao().queryRow(
				"select count(id) as count_num from " + DBTable.sensitiveLog + " " + where,
				whereParamList.subList(0, whereParamList.size() - 2).toArray());
		int recordsTotal = 0;
		if (countRow.get("count_num") != null) {
			recordsTotal = new Integer(countRow.get("count_num").toString()).intValue();
		}

		// 拼装返回的数据
		data.put("data", dataList.toArray());
		data.put("itemsCount", recordsTotal);

		return WebUtil.resultString(data, request);
	}
	
	@RequestMapping(value="/filterTask",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String filterTask(HttpServletRequest request){

		Map result=new HashMap();
		result.put("result",1);
		result.put("msg", "操作成功");
		
		this.getSensitiveKeywordsService().filterTask();
		
		
		return WebUtil.resultString(result,request);
	}
	
	@RequestMapping(value="/recheckData",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String recheckData(HttpServletRequest request){

		Map result=new HashMap();
		int id=Integer.parseInt(request.getParameter("id").toLowerCase());
		
		Map record=this.getCommonService().getJdbcDao().queryRow("select * from "+DBTable.sensitiveLog+" where id=?", new Object[] {id});
		
		if(record!=null) {
			String keywords=this.getSensitiveKeywordsService().filterSensitiveKeywords(record.get("data_type").toString(),Integer.parseInt(record.get("data_id").toString()),true);
			if(keywords==null) {
				//设置为已处理状态
				this.getCommonService().getJdbcDao().updateSql("update "+DBTable.sensitiveLog+" set status=1 where id=?",new Object[] {id});
				result.put("result",1);
				result.put("msg", "已处理完成，未发现敏感词");
			}else {
				//更新敏感词和检查时间字段
				this.getCommonService().getJdbcDao().updateSql("update "+DBTable.sensitiveLog+" set sensitive_keywords=?,create_time=? where id=?",new Object[] {keywords,new Date(),id});
				result.put("result",1);
				result.put("msg", "发现敏感词:"+keywords);
			}
		}else {
			result.put("result",0);
			result.put("msg", "数据不存在");
		}
		
		return WebUtil.resultString(result,request);
	}
	
	@RequestMapping(value="/deleteData",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String deleteData(HttpServletRequest request){

		Map result=new HashMap();
		int id=Integer.parseInt(request.getParameter("id").toLowerCase());
		
		Map record=this.getCommonService().getJdbcDao().queryRow("select * from "+DBTable.sensitiveLog+" where id=?", new Object[] {id});
		
		if(record!=null) {
			//将数据设置为删除状态
			this.getCommonService().getJdbcDao().updateSql("update "+record.get("data_type")+" set del_flag=1 where id=?",new Object[] {record.get("data_id")});
			
			//设置为已删除状态
			this.getCommonService().getJdbcDao().updateSql("update "+DBTable.sensitiveLog+" set status=2 where id=?",new Object[] {id});
			
			result.put("result",1);
			result.put("msg", "操作成功");
		}else {
			result.put("result",0);
			result.put("msg", "数据不存在");
		}
		
		
		
		
		return WebUtil.resultString(result,request);
	}
	
	
	

	

}
